<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}
/**
 * Class account extends MY_Controller
 * @author hanj
 * 移动端用户接口类(用户类, 店长等身份用)
 */
class regist extends API_Controller {
	protected $com_id = 0;
	protected $company_uid = 0;
	public function __construct() {
		parent::__construct();
		$this->load->model('upload_model','mupload');
		$this->load->model('coltypes_model','mcoltypes');
		$this->load->model('account_info_model','maccountInfo');
		$this->load->model('company_model','mcompany');
		$this->userInfoFields = 'id,phone,nickname';
	}
	protected $rules = array(
		"go" => array(
			array(
				"field" => "uname",
				"label" => "姓名",
				"rules" => "trim|required|min_length[2]|max_length[15]|is_chinese",
			)
			, array(
				"field" => "phone",
				"label" => "手机号码",
				"rules" => "trim|required|numeric|min_length[11]|max_length[11]|is_mobile|callback_account_check",
			),
			array(
				"field" => "position",
				"label" => "职位",
				"rules" => "trim|required",
			)
			, array(
				"field" => "company_name",
				"label" => "公司全称",
				"rules" => "trim|required|callback_comName_check",
			)
			, array(
				"field" => "short",
				"label" => "公司简称",
				"rules" => "trim|required",
			)
			, array(
				"field" => "smscode",
				"label" => "手机验证码",
				"rules" => "trim|required|min_length[6]|max_length[6]|callback_smscode_check",
			)
			, array(
				"field" => "pwd",
				"label" => "登录密码",
				"rules" => "trim|required|min_length[6]|max_length[12]",
			)
			, array(
				"field" => "repwd",
				"label" => "确认密码",
				"rules" => "trim|required|min_length[6]|max_length[12]|matches[pwd]",
			)
			, array(
				"field" => "card",
				"label" => "名片",
				"rules" => "trim|required|callback_photo_check",
			),
			array(
				"field" => "photo",
				"label" => "名片",
				"rules" => "trim|required|callback_photo_check",
			)
			, array(
				"field" => "ctype",
				"label" => "用户类别",
				"rules" => "trim|required|callback_ctype_check",
			)
		),
		// 注册时用
		"smsCode" => array(
			array(
				'field' => 'phone',
				'label' => '手机号',
				'rules' => 'trim|required|numeric|min_length[11]|max_length[11]|is_mobile|callback_account_check',
			),
		),
		// 忘记密码
		"forget_sms" => array(
			array(
				'field' => 'phone',
				'label' => '手机号',
				'rules' => 'trim|required|numeric|min_length[11]|max_length[11]|is_mobile|callback_faccount_check',
			),
		),
		// 忘记密码
		"forget" => array(
			array(
				'field' => 'phone',
				'label' => '手机号',
				'rules' => 'trim|required|numeric|min_length[11]|max_length[11]|is_mobile|callback_faccount_check',
			),
			array(
				"field" => "smscode",
				"label" => "手机验证码",
				"rules" => "trim|required|min_length[6]|max_length[6]|callback_smscode_check",
			),
			array(
				"field" => "pwd",
				"label" => "新密码",
				"rules" => "trim|required|min_length[6]|max_length[12]",
			),
			array(
				"field" => "repwd",
				"label" => "确认密码",
				"rules" => "trim|required|min_length[6]|max_length[12]|matches[pwd]",
			)
		),
	);

	// 用户注册
	public function go() {
		// 返回服务器时间以及预定义参数
		$this->vdata['timeline'] = time();
		$this->vdata['content'] = '';
		$this->vdata['secure'] = 0;
		if($this->data['ctype'] == 3) {
			unset($this->rules['go'][2]);
			unset($this->rules['go'][3]);
			unset($this->rules['go'][4]);
		}
		// 验证
		$this->form_validation->set_rules($this->rules['go']);

		// validate验证结果
		if ($this->form_validation->run('', $this->data) == false) {
			// 返回失败
			$this->vdata['returnCode'] = '0011';
			$this->vdata['returnInfo'] = validation_errors();
		} else {
			$photo = $this->mupload->get_one($this->data['photo']);
			$account_data = array(
				'phone' => $this->data['phone'],
				'ctype' => $this->data['ctype'],
				'uname' => $this->data['uname'],
				'nickname' => $this->data['uname'],
				'photo' => $this->data['photo'],
				'thumb' => UPLOAD_URL.$photo['thumb'],
				'pwd' => passwd($this->data['pwd']),
				'position' => isset($this->data['position']) ? $this->data['position'] : '',
				'board' => isset($this->data['board']) ? $this->data['board'] : '',
				'reg_time' => time(),
				'inviter' => $this->data['inviter'],
			);
			$card = $this->mupload->get_one($this->data['card']);
			$info_data = array(
				'card' => $this->data['card'],
				'ctype' => $this->data['ctype'],
				'thumb' => UPLOAD_URL.$card['thumb'],
			);
			if($this->data['ctype'] == 1 ) {
				/*$company_data = array(
					'name' => $this->data['company_name'],
					'short' => $this->data['short'],
					'initials' => pinyin($this->data['company_name'],'UTF8',true)
				);
				$company = $this->mcompany->get_one($company_data);
				if(!$company) {
					$company_id = $this->mcompany->create($company_data);
					$company_uid = 0;
				} else {
					$company_id = $company['id'];
					$company_uid = $company['uid'];
				}*/
				$account_data['company'] = $this->com_id;
			} else {
				if(isset($this->data['company_name']) && $this->data['company_name']) {
					$account_data['com_name'] = $this->data['company_name'];
				}
				if(isset($this->data['short']) && $this->data['short']) {
					$account_data['com_short'] = $this->data['short'];
				}
			}

			// 创建数据
			if ($id = $this->macc->create($account_data)) {
				if (!isset($this->data['terminalNo'])) {
					$this->data['terminalNo'] = 3;
				}
				if($this->data['ctype'] == 1 && !$this->company_uid) {
					$this->mcompany->update(array('uid'=>$id),array(
						'id' => $this->com_id
					));
				}
				$info_data['uid'] = $id;
				$this->maccountInfo->create($info_data);
				// 返回用户详细数据
				if ($info = $this->mvacc->get_info(array('id' => $id), 'fresh' , $this->data['terminalNo'], $this->userInfoFields)) {

					$this->vdata['returnCode'] = '200';
					$this->vdata['returnInfo'] = '操作成功';
					$this->vdata['secure'] = JSON_SECURE;
					$this->vdata['content'] = $info;
				} else {
					$this->vdata['returnCode'] = '200';
					$this->vdata['returnInfo'] = '操作失败';
				}
			} else {
				$this->vdata['returnCode'] = '200';
				$this->vdata['returnInfo'] = '操作失败';
			}
		}

		// 返回json数据
		$this->_send_json($this->vdata);
	}

	// 发送验证码(创建用户时用)
	public function smsCode() {
		// 返回服务器时间以及预定义参数
		$this->vdata['timeline'] = time();
		$this->vdata['content'] = '';
		$this->vdata['secure'] = 0;
		if(isset($this->data['forget']) && $this->data['forget']) {
			$this->form_validation->set_rules($this->rules['forget_sms']);
			$ctype =2;
		} else {
			// 验证
			$this->form_validation->set_rules($this->rules['smsCode']);
			$ctype =1;
		}

		// validate验证结果
		if ($this->form_validation->run('', $this->data) == false) {
			// 返回失败
			$this->vdata['returnCode'] = '0011';
			$this->vdata['returnInfo'] = validation_errors();
		} else {
			$this->load->model('smscode_model', 'msmscode');
			if ($content = $this->msmscode->sendCode($this->data['phone'], 1,$ctype)) {
				if ($content['code'] == 421) {
					$this->vdata['returnCode'] = '0011';
					$this->vdata['returnInfo'] = $content['msg'];
					$this->vdata['secure'] = JSON_SECURE;

				} else {
					// 返回成功
					$this->vdata['returnCode'] = '200';
					$this->vdata['returnInfo'] = '操作成功';
					$this->vdata['secure'] = JSON_SECURE;
					$this->vdata['content'] = $content['msg'];
				}

			} else {
				// 返回成功
				$this->vdata['returnCode'] = '0011';
				$this->vdata['returnInfo'] = '短信发送失败';
				$this->vdata['secure'] = JSON_SECURE;
				$this->vdata['content'] = $content;
			}
		}

		// 返回json数据
		$this->_send_json($this->vdata);
	}
	public function forget() {

		$this->form_validation->set_rules($this->rules['forget']);

		// validate验证结果
		if ($this->form_validation->run('', $this->data) == false) {
			// 返回失败

			$this->vdata['returnCode'] = '0011';
			$this->vdata['returnInfo'] = validation_errors();
		} else {
			$account = $this->macc->get_one(array('phone'=>$this->data['phone']),'id');
			if ($res = $this->macc->set($account['id'], array('pwd' => passwd($this->data['pwd'])))) {
				// 返回成功
				$this->vdata['returnCode'] = '200';
				$this->vdata['returnInfo'] = '操作成功,请登录';
				$this->vdata['secure'] = JSON_SECURE;
				$this->vdata['content']= $res;
			} else {
				// 返回失败
				$this->vdata['returnCode'] = '0011';
				$this->vdata['returnInfo'] = '更新失败,请刷新后重试';
				$this->vdata['secure'] = JSON_SECURE;
				$this->vdata['content'] = '';
			}
		}
		// 返回json数据
		$this->_send_json($this->vdata);
	}

	//////////////////////////////////////
	///////////////规则验证////////////////
	//////////////////////////////////////

	// 检验找回密码账户
	public function account_check($account) {
		// 查找用户
		if ($this->info = $this->macc->get_one(array('phone' => $account,'status <>' => 0,'audit'=>0))) {
			$this->form_validation->set_message('account_check', '该手机号已经被注册！');
			return false;
		} else {
			return true;
		}
	}
	// 检验找回密码账户
	public function faccount_check($account) {
		// 查找用户
		if ($info = $this->macc->get_one(array('phone' => $account))) {
			if($info['audit']) {
				$this->form_validation->set_message('faccount_check', '此手机号已被禁用！');
				return false;
			}
			if(!$info['status']) {
				$this->form_validation->set_message('faccount_check', '此手机号帐户未审核通过！');
				return false;
			}
			return true;
		} else {
			$this->form_validation->set_message('faccount_check', '无此手机号！');
			return false;
		}
	}
	//检查图片
	public function photo_check($photo) {
		// 查找用户
		$photo_check = $this->mupload->get_one($photo);
		if (!$photo_check) {
			$this->form_validation->set_message('photo_check', '上传图片路径有误');
			return false;
		} else {
			return true;
		}
	}
	//检查用户身份
	public function ctype_check($ctype){
		$type = $this->mcoltypes->get_one($ctype);
		if (!$type) {
			$this->form_validation->set_message('ctype_check', '选择用户分类有误');
			return false;
		} else {
			return true;
		}
	}

	public function smscode_check($smscode) {
		$this->load->model('smscode_model', 'msmscode');

		if (!$this->msmscode->verifySmsCode($this->data['phone'], $smscode, 1)) {
			// 返回失败
			$this->form_validation->set_message('smscode_check', '验证码验证失败,请重新填写');
			return false;
		}


		return true;
	}
	/***
	 * 只要全称存在就绑定
	如果全称不存在简称存在，提示简称已被使用
	如果两个都不存在，生成
	 * 验证公司名称，只对于企业用户
	 */
	public function comName_check($name) {
		if($this->data['ctype'] == 1) {
			$company = $this->mcompany->get_one(array('name'=>$name),'id,uid');
			if($company) {
				$this->load->model('company_board_model','mcompany_board');
				$stock_code = $this->mcompany_board->get_one(array('name'=>$name),'stock_code');
				$this->com_id = $company['id'];
				$this->company_uid = $company['uid'];
				$this->mcompany->update(array('stock_code'=>$stock_code['stock_code']),array('id'=>$company['id']));
				return true;
			} else {
				$short = $this->mcompany->get_one(array('short'=>$this->data['short']));
				if($short) {
					$this->form_validation->set_message('comName_check', '简称已被使用!!');
					return false;
				} else {
					$company_data = array(
						'name' => $this->data['company_name'],
						'short' => $this->data['short'],
						'initials' => pinyin($this->data['short'],'UTF8',true)
					);
					$this->com_id = $this->mcompany->create($company_data);

					return true;
				}
			}
		} else {
			return true;
		}

	}
}
